package com.bac.utils;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bac.bacplatform.R;
import com.bac.javabeans.Bill;

public class BillListAdapter extends BaseAdapter {
	LayoutInflater inflater;
	ViewHoder viewHoder;
	List<Bill> lb;

	public BillListAdapter(Context context, List<Bill> lb) {
		inflater = LayoutInflater.from(context);
		this.lb = lb;
	}

	@Override
	public View getView(int count, View convertView, ViewGroup viewGroup) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.bill_item, null);
			viewHoder = new ViewHoder();
			viewHoder.tag = (TextView) convertView.findViewById(R.id.bill_item_tag);
			viewHoder.tag.setVisibility(View.GONE);
			viewHoder.name = (TextView) convertView.findViewById(R.id.bill_name);
			viewHoder.time = (TextView) convertView.findViewById(R.id.bill_time);
			viewHoder.money = (TextView) convertView.findViewById(R.id.bill_money);
			viewHoder.type = (TextView) convertView.findViewById(R.id.bill_type);
			convertView.setTag(viewHoder);
		}else{
			viewHoder = (ViewHoder) convertView.getTag();
		}

		String money = "-"+lb.get(count).getAmount().doubleValue() + "";

//		if (lb.get(count).getAmount().doubleValue() > 0) {
//			money = "-" + lb.get(count).getAmount().doubleValue();
//		}

		try {

			if (count == 0) {
				viewHoder.tag.setVisibility(View.VISIBLE);
				viewHoder.tag.setText(Util.getMonthNum(
						lb.get(count).getTransactionTime())
						+ "月");
			} else if (Util.getMonthNum(lb.get(count).getTransactionTime()) != Util.getMonthNum(
					lb.get(count - 1).getTransactionTime())) {
				viewHoder.tag.setVisibility(View.VISIBLE);
				viewHoder.tag.setText(Util.getMonthNum(
						lb.get(count).getTransactionTime())
						+ "月");
			}else{
				viewHoder.tag.setVisibility(View.GONE);
			}
			
				viewHoder.money.setText(money);
				viewHoder.name.setText(lb.get(count).getCode());
				viewHoder.time.setText(Util.getMonthNum(
						lb.get(count).getTransactionTime())
						+ "月"
						+ Util.getDayNum(lb.get(count).getTransactionTime())
						+ "日"
						+ Util.getHourNum(lb.get(count).getTransactionTime())
						+ ":"
						+ Util.getMinuNum(lb.get(count).getTransactionTime()));
			   viewHoder.type.setText("油卡到账  +"+lb.get(count).getActualAmount());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	class ViewHoder {
		TextView tag;
		TextView name;
		TextView time;
		TextView money;
		TextView type;
	}

	@Override
	public int getCount() {
		return lb.size();
//		return 0;
	}

	@Override
	public Object getItem(int i) {
		return null;
	}

	@Override
	public long getItemId(int i) {
		return 0;
	}

}
